package top.laoshuzi.designpatterns.base.structure.composite;

import java.util.HashMap;
import java.util.Map;

/**
 * 组合
 */
public class Composite implements Component {

    private String name;
    private Map<String, Component> unitList = new HashMap<>();

    public Composite(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void add(Component component) {
        unitList.put(component.getName(), component);
    }

    @Override
    public void remove(Component component) {
        unitList.remove(component.getName());
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("Composite{");
        sb.append("name='" + name + '\'');
        for (String key : unitList.keySet()) {
            Component component = unitList.get(key);
            sb.append('\n');
            sb.append(component.toString());
        }
        sb.append('\n');
        sb.append('}');
        return sb.toString();
    }
}
